const logger = require('../../utils/log');
const http = require('http');
const querystring = require('querystring');

const server = http.createServer((request, response, next) => {
    // const url = request.url;

    logger.debug(next);
    let data = '';
    request.on('data', (chunk) => {
        // logger.debug(chunk);
        data += chunk;
    })
    request.on('end', () => {
        response.writeHead(200, {
            'content-type': 'application/json;charset=utf-8'
        })
        // logger.debug(data);
        response.write(JSON.stringify(querystring.parse(data)));
        response.end();
    })

    // response.writeHead(200, {
    //     // 'content-type': 'text/plain'
    //     // 'content-type': 'text/html'
    //     'content-type': 'application/json;charset=utf-8'
    // })
    // response.write('<div>hello world</div>');
    // response.write('{"name": "Tina"}');
    // response.write(`{"url": "${url}"}`);
    // response.end();

    // 不要write只要end也可以
    // response.end('{"name": "Tina"}');
})

server.listen(8080, () => {
    console.log('localhost:8080');
})
